# -*- coding: utf-8 -*-
import json
import os


class Book:
    def __init__(self, title, author, isbn, available=True):  # 新增available参数，设置默认值
        self.title = title
        self.author = author
        self.isbn = isbn
        self.available = available  # 使用传入的值或默认值

    def __str__(self):
        status = "可借阅" if self.available else "已借出"
        return f"书名：{self.title}\n作者：{self.author}\nISBN：{self.isbn}\n状态：{status}"


class Library:
    def __init__(self, file_path='library_data.json'):
        self.file_path = file_path
        self.books = self._load_data()

    def _load_data(self):
        if os.path.exists(self.file_path):
            with open(self.file_path, 'r') as f:
                data = json.load(f)
                return [Book(**book) for book in data]
        return []

    def _save_data(self):
        with open(self.file_path, 'w') as f:
            data = [{'title': book.title, 'author': book.author, 'isbn': book.isbn, 'available': book.available} for book in self.books]
            json.dump(data, f, indent=4)

    def add_book(self, book):
        if any(b.isbn == book.isbn for b in self.books):
            print("错误：该ISBN已存在")
            return
        self.books.append(book)
        self._save_data()
        print("图书添加成功！")

    def remove_book(self, isbn):
        for i, book in enumerate(self.books):
            if book.isbn == isbn:
                del self.books[i]
                self._save_data()
                print("图书删除成功！")
                return
        print("错误：未找到该ISBN的图书")

    def list_books(self):
        if not self.books:
            print("当前图书馆没有图书")
            return
        for book in self.books:
            print(book)
            print("-" * 30)

    def search_book(self, keyword):
        results = []
        for book in self.books:
            if keyword.lower() in book.title.lower() or \
               keyword.lower() in book.author.lower():
                results.append(book)
        
        if not results:
            print("未找到相关图书")
            return
        
        print(f"找到 {len(results)} 本相关图书：")
        for book in results:
            print(book)
            print("-" * 30)


def main():
    library = Library()
    
    while True:
        print("\n=== 图书馆管理系统 ===")
        print("1. 添加图书")
        print("2. 删除图书")
        print("3. 显示所有图书")
        print("4. 搜索图书")
        print("5. 退出系统")
        
        choice = input("请输入选项：")
        
        if choice == '1':
            title = input("请输入书名：").strip()
            author = input("请输入作者：").strip()
            isbn = input("请输入ISBN：").strip()
            if title and author and isbn:
                library.add_book(Book(title, author, isbn))
            else:
                print("错误：所有字段不能为空")
        
        elif choice == '2':
            isbn = input("请输入要删除的图书ISBN：")
            library.remove_book(isbn)
        
        elif choice == '3':
            library.list_books()
        
        elif choice == '4':
            keyword = input("请输入书名或作者进行搜索：")
            library.search_book(keyword)
        
        elif choice == '5':
            print("感谢使用！")
            break
        
        else:
            print("无效的输入，请重新选择")


if __name__ == "__main__":
    main()
    